/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.domain.repository;

import org.cshoo.tattoo.authorize.domain.po.Button;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IButtonRepository extends CrudRepository<Button, Long> {

    @Query("update Button button set button.name = :#{#button.name}, button.description = :#{#button.description} where button.id = :#{#button.id}")
    @Transactional
    @Modifying
    void updateButton(Button button);

    List<Button> findButtonsByMenuId(Long menuId);

    Button findByCodeAndMenuId(String code, Long menuId);

    @Query("select bt from Button bt inner join RoleButtonRelation rbr on bt.id = rbr.buttonId where rbr.roleId = ?1")
    List<Button> findByRoleId(Long roleId);

    @Query("select bt from Button bt inner join RoleButtonRelation rbr on bt.id = rbr.buttonId where rbr.roleId in ?1 and bt.menuId in ?2")
    List<Button> findByRoleIdsAndMenuIds(List<Long> roleIds, List<Long> menuIds);
}
